/*CoreUtil*/
/*工具类，类似java静态工具类*/
var CoreUtil = (function() {
  var coreUtil = {};
  /* ajax请求 */
  coreUtil.sendAjax = function(url, params, ft, failFt, method, headers, noAuthorityFt, contentType, async) {
    $.ajax({
      url : url,
      cache : false,
      async : async == undefined ? true : async,
      data : params,
      type : method == undefined ? "POST" : method,
      contentType : contentType == undefined ? 'application/json; charset=UTF-8' : contentType,
      dataType : "json",
      success : function(res) {
        console.log("JSON.stringify(res):" + JSON.stringify(res))
        if (typeof ft == "function") {
          if (res.code == 0) {
            if (ft != null && ft != undefined) {
              ft(res);
            }
          } else {
        	  toastr.error(res.msg)          
          }
        }
      },
      error : function(XMLHttpRequest, textStatus, errorThrown) {
        if (XMLHttpRequest.status == 404) {
          //top.window.location.href = "/404.html";
          toastr.error("404错误！请检查页面服务地址！");
        } else {
          toastr.error("服务器好像除了点问题！请稍后试试");
        }
      }
    });
  };
  /* 表单数据封装成 json String */
  coreUtil.formJson = function(frm) { // frm：form表单的id
    var o = {};
    var a = $("#" + frm).serializeArray();
    $.each(a, function() {
      if (o[this.name] !== undefined) {
        if (!o[this.name].push) {
          o[this.name] = [ o[this.name] ];
        }
        o[this.name].push(this.value || '');
      } else {
        o[this.name] = this.value || '';
      }
    });
    return JSON.stringify(o);
  };
  /* 存入本地缓存 */
  coreUtil.setData = function(key, value) {
    layui.data('LocalData', {
      key : key,
      value : value
    })
  };
  /* 从本地缓存拿数据 */
  coreUtil.getData = function(key) {
    var localData = layui.data('LocalData');
    return localData[key];
  };

  coreUtil.formattime = function(val) {
    var date = new Date(val);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    month = month > 9 ? month : ('0' + month);
    var day = date.getDate();
    day = day > 9 ? day : ('0' + day);
    var hh = date.getHours();
    hh = hh > 9 ? hh : ('0' + hh);
    var mm = date.getMinutes();
    mm = mm > 9 ? mm : ('0' + mm);
    var ss = date.getSeconds();
    ss = ss > 9 ? ss : ('0' + ss);
    var time = year + '-' + month + '-' + day + ' ' + hh + ':' + mm + ':' + ss;
    return time;
  };
  return coreUtil;
})(CoreUtil, window);
